// map.js
import qqmap from '../../../utils/qqmapsdk'
Page({
  data: {
    latitude: 40.060539,
    longitude: 116.343847,
    polyline: [],
    id: '',
    scale: 16,
    markers: [
      {
        id: 1,
        latitude: 40.060539,
        longitude: 116.343847,
        width: 30,
        height: 40
      },
      {
        id: 2,
        latitude: 40.061539,
        longitude: 116.341847,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40
      },
    ],
    detilList: []
  },
  onLoad(e) {
    // console.log(e);
    this.setData({
      id: e.id
    })
    this.getRepairDetil(e.id)
    this.getRoad()
  },
  // 取消报修
  async removeRepair() {
    const res = await wx.http.put(`/cancel/repaire/${this.data.id}`)
    // console.log(res);
    wx.navigateTo({
      url: '/repair_pkg/pages/list/index',
    })

  },
  // 获取路径规划信息
  getRoad() {
    // console.log(qqmap);
    qqmap.direction({
      mode: 'bicycling',
      from: {
        latitude: 40.061539,
        longitude: 116.341847,
      },
      to: {
        latitude: 40.060539,
        longitude: 116.343847,
      },
      success: (res, err) => {
        console.log(res, err);
        const coors = res.result.routes[0].polyline
        // 解压 返回的数据
        for (let i = 2; i < coors.length; i++) { coors[i] = coors[i - 2] + coors[i] / 1000000 }
        const points = []
        // 拼好解压回来的经纬度
        for (var i = 0; i < coors.length; i += 2) {
          points.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        // console.log(points);
        this.setData({
          // 配置线条颜色宽度 
          polyline: [{
            points,
            color: '#4d90fe',
            width: 4
          }]
        })

      }
    })
  },
  //跳转修改信息
  goChange(e) {
    wx.redirectTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.id,
    })
  },
  // 获取报修详情信息
  async getRepairDetil(id) {
    const res = await wx.http.get(`/repair/${id}`)
    console.log(res);
    this.setData({
      detilList: res.data
    })
  }
})
